package com.sell.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sell.demo.entity.Region;
import com.sell.demo.entity.RegionRepository;

/**
 * Created by Mingo on 2017/6/5.
 */
@RestController
@RequestMapping(value = "/region")
@Api(value = "国家行政区划相关接口")
public class RegionController {

    private final RegionRepository regionRepository;

    @Autowired
    public RegionController(RegionRepository regionRepository) {
        this.regionRepository = regionRepository;
    }


    @RequestMapping(value = "/getProvinceList", method = RequestMethod.GET)
    @ApiOperation(value = "省列表", response = Region.class, responseContainer = "List")
    public Object getProvinceList() {
        return regionRepository.findAllProvince();
    }

    @RequestMapping(value = "/getCityList", method = RequestMethod.GET)
    @ApiOperation(value = "城市列表", response = Region.class, responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", paramType = "query", dataType = "String", required = false, value = "省级行政区划代码", defaultValue = "110000")
    })
    public Object getCityList(@RequestParam(value = "code", required = false) String code) {
        return regionRepository.findCityList(code);
    }

    @RequestMapping(value = "/getCountyList", method = RequestMethod.GET)
    @ApiOperation(value = "区县列表", response = Region.class, responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", paramType = "query", dataType = "String", required = false, value = "市级行政区划代码", defaultValue = "110100")
    })
    public Object getAreaList(@RequestParam(value = "code", required = false) String code) {
        return regionRepository.findCountyList(code);
    }

    @RequestMapping(value = "/getRegionByCode", method = RequestMethod.GET)
    @ApiOperation(value = "区县列表", response = Region.class, responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", paramType = "query", dataType = "String", required = true, value = "行政区划代码", defaultValue = "110101")
    })
    public Object getRegionByCode(@RequestParam(value = "code") String code) {
        return regionRepository.findRegionByCode(code);
    }
}
